package com.zhangye.test.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhangye.test.mapper.AdminMapper;
import com.zhangye.test.mapper.AdminMoneyMapper;
import com.zhangye.test.mapper.MoneyTypeMapper;
import com.zhangye.test.mapper.WatchRecordMapper;
import com.zhangye.test.model.Admin;
import com.zhangye.test.model.AdminMoney;
import com.zhangye.test.model.EasyAr;
import com.zhangye.test.model.MoneyType;
import com.zhangye.test.model.WatchRecord;
import com.zhangye.test.util.PageInfo;

@Service
public class AdminMoneyService {

	@Autowired
	private AdminMoneyMapper adminMoneyMapper;
	@Autowired
	private AdminMapper adminMapper;
	@Autowired
	private WatchRecordMapper watchRecordMapper;
	@Autowired
	private MoneyTypeMapper moneyTypeMapper;
	
	
	@Transactional
	public double updateMoney(Admin admin,EasyAr ea,String type){
		double money = 0;
		WatchRecord wr = new WatchRecord();
		wr.setAdminId(admin.getId());
		wr.setEasyArId(ea.getId());
		wr.setCreatedAt(new Date());
		watchRecordMapper.insert(wr);
		String where = " and TO_DAYS(created_at) = TO_DAYS(NOW()) and admin_id = " + admin.getId();
		List<WatchRecord> list = watchRecordMapper.selectByWhere(where);
		int count = list.size();
		if(count%3 == 0){//给
			where = " and admin_id = " + admin.getId() + " and TO_DAYS(created_date) = TO_DAYS(NOW())";
			List<AdminMoney> listAm = adminMoneyMapper.selectByWhere(where);
			//今日得积分
			double todayMoney = 0;
			for(int i = 0 ; i < listAm.size() ; i ++){
				double todayGetMoney = listAm.get(i).getConsumeMoney();
				if(todayGetMoney > 0){
					todayMoney += todayGetMoney;
				}
			}
			List<MoneyType> listMt = moneyTypeMapper.selectByWhere(" and type = 2");//当天最大分值
			if(listMt.size() > 0){
				int maxMoney = listMt.get(0).getMaxMoney();
				if(todayMoney < maxMoney - 3){//可再次获取积分
					listMt = moneyTypeMapper.selectByWhere(" and type = 0");
					if(listMt != null){
						money = this.getRandom(listMt.get(0).getMinMoney(), listMt.get(0).getMaxMoney());//本次获得的积分
						AdminMoney am = new AdminMoney();
						am.setAdminId(admin.getId());
						am.setEasyArId(ea.getId());
						am.setConsumeDate(new Date());
						am.setCreatedDate(new Date());
						am.setConsumeMoney(money);
						am.setType(type);
						double adminMoney = admin.getMoney();
						am.setMoneyTotal(money + adminMoney);
						admin.setMoney(money + adminMoney);
						adminMoneyMapper.insert(am);
						adminMapper.updateByPrimaryKey(admin);
					}
				}
			}
		}
		return money;
	}
	
	
	private int getRandom(int min,int max){
		return (int) (Math.random() * (max-min) + min + 1);
	}
	
	
	
	public List<AdminMoney> selectByWhere(String where){
		List<AdminMoney> list = adminMoneyMapper.selectByWhere(where);
		return list;
	}
	
	
	public List<Map<String,Object>> selectByPage(PageInfo pageInfo, String where, String order) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("myCount", pageInfo.getCount());
		param.put("pageSize", pageInfo.getPageSize());
		param.put("page", pageInfo.getPage());
		param.put("where", where);
		param.put("order", order);
		List<Map<String, Object>> m = adminMoneyMapper.selectByPage(param);
		pageInfo.setCount(Integer.parseInt(param.get("myCount").toString()));
		return m;
	}
	
	
	/**
	 * 添加明细，修改管理员总金额
	 * 作者： 张叶
	 * 创建时间：2018年10月18日  下午4:21:01
	 */
	@Transactional
	public int insert(AdminMoney am,Admin admin){
		double cm = am.getConsumeMoney();
		double money = admin.getMoney();
		money = money + cm;
		am.setMoneyTotal(money);
		admin.setMoney(money);
		int i = adminMoneyMapper.insert(am);
		i += adminMapper.updateByPrimaryKey(admin);
		return i;
	}
}
